package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.ResultInfo;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-10
 */
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public ResultInfo addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return ResultInfo.ok();
    }

    //根据小节id删除小节，删除对应阿里云视频

    @DeleteMapping("{id}")
    public ResultInfo deleteVideo(@PathVariable String id) {
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            //删除小节视频
            ResultInfo resultInfo = vodClient.removeAlyVideo(videoSourceId);
            if (resultInfo.getCode()==20001){
                throw new GuliException(20001,"删除视频失败！");
            }
        }
        videoService.removeById(id);
        return ResultInfo.ok();
    }

    //根据小节id查询
    @GetMapping("getVideoInfo/{videoId}")
    public ResultInfo getVideoInfo(@PathVariable String videoId) {
        EduVideo eduVideo = videoService.getById(videoId);
        return ResultInfo.ok().data("video",eduVideo);
    }

    //修改小节
    @PostMapping("updateVideoInfo")
    public ResultInfo updateVideo(@RequestBody EduVideo eduVideo) {
        videoService.updateById(eduVideo);
        return ResultInfo.ok();
    }


}

